package com.xinsoft.controller.wms;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xinsoft.common.Response;
import com.xinsoft.entity.dto.ScmProduceInstockDTO;
import com.xinsoft.entity.dto.ScmProduceInstockParam;
import com.xinsoft.entity.vo.ScmProduceInstockPageVo;
import com.xinsoft.entity.vo.ScmProduceInstockVo;
import com.xinsoft.service.ScmProduceInstockService;
import com.xinsoft.utils.ExcelUtils;
import com.xinsoft.validator.ValidatorUtils;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 生产入库
 * @folder 云供应链/仓库管理/生产入库
 */
@RestController
@RequestMapping("/scm/produceInstock")
public class ScmProduceInstockController {
    @Autowired
    private ScmProduceInstockService produceInstockService;

    /**
     * 分页
     */
    @PostMapping("/pageList")
    public Response<?> pageList(@RequestBody ScmProduceInstockParam param) {
        return Response.succeed(produceInstockService.findPageListByParam(param));
    }


    /**
     * 暂存
     */
    @PostMapping("/addOrUpdate")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> addOrUpdate(@RequestBody ScmProduceInstockDTO dto) {
        // 参数校验
        ValidatorUtils.validateEntity(dto);
        produceInstockService.tempSave(dto);
        return Response.succeed();
    }

    /**
     * 获取详情
     */
    @GetMapping("/detail")
    public Response<ScmProduceInstockVo> detail(@RequestParam(value = "id", defaultValue = "") Integer id) {
        return Response.succeed(produceInstockService.detail(id));
    }

    /**
     * 删除
     */
    @PostMapping("/del")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> del(@RequestBody List<Integer> ids) {
        ids = ids.stream().distinct().collect(Collectors.toList());
        produceInstockService.del(ids);
        return Response.succeed();
    }

    /**
     * 导出
     */
    @PostMapping("/export")
    public Response<?> export(@RequestBody @ApiParam("查询参数") ScmProduceInstockParam param, HttpServletResponse response) throws Exception {
        List<ScmProduceInstockPageVo> list = produceInstockService.findListByParam(param);
        ExcelUtils.exportByEasypoi(list, ScmProduceInstockPageVo.class, "生产入库", "生产入库", response, "生产入库");
        return Response.succeed();
    }

    /**
     * 审核
     */
    @GetMapping("/audit")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> audit(@ApiParam(value = "采购入库单id",required = true) @RequestParam("id") Integer id){
        produceInstockService.audit(id);
        return Response.succeed();
    }

    /**
     * 反审核
     */
    @GetMapping("/reverseAudit")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> reverseAudit(@ApiParam(value = "采购入库单id",required = true) @RequestParam("id") Integer id){
        produceInstockService.reverseAudit(id);
        return Response.succeed();
    }

    /**
     * 保存并审核
     */
    @PostMapping("/saveAudit")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> saveAudit(@RequestBody ScmProduceInstockDTO dto){
        ValidatorUtils.validateEntity(dto);
        produceInstockService.saveAudit(dto);
        return Response.succeed();
    }

}
